<template>
<div>
  <div id="nav">
      <router-link to="/">登录</router-link> |
      <router-link to="/register">注册</router-link>
    </div>

  <div id="register">
    <el-card class="box-card" shadow="hover">
      <div slot="header" class="clearfix">
        <span>用户注册</span>
      </div>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="用户名">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="form.nikname"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input
              placeholder="请输入密码"
              v-model="form.password"
              show-password
            ></el-input>
          </el-form-item>
            <el-button type="primary" @click="onSubmit" class="regBtn">立即注册</el-button>
        </el-form>
        <span>已有账号？<router-link to="/">去登录</router-link></span>
    </el-card>
  </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'register',
  data() {
    return {
      form: {
        name: '',
        nikname: '',
        password: ''
      },
      errMsg: ''
    }
  },
  methods: {
    onSubmit() {
      const that = this
      // console.log('submit!')
      console.log(this.form.name)
      axios
        .post('http://47.110.234.216:7001/register', {
          username: this.form.name,
          nikname: this.form.nikname,
          password: this.form.password
        })
        .then(function (res) {
          console.log(res)
          that.errMsg = '注册失败！'

          // eslint-disable-next-line eqeqeq
          if (res.data.status == true) {
            that.$message({
              showClose: true,
              message: '注册成功',
              type: 'success'
            })
            that.$router.push('/')
            return
          }
          that.$message({
            showClose: true,
            message: that.errMsg,
            type: 'error'
          })
        })
    }
  }
}
</script>

<style scoped>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }

  .clearfix:after {
    clear: both;
  }

  .box-card {
    width: 480px;
    margin: auto;
  }

  .regBtn {
    margin-bottom: 20px;
  }

  a {
    text-decoration: none;
    color: orange;
  }

  .router-link-active {
    text-decoration: none;
  }

  </style>
